package com.project.common;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ExceptionLogger {

	/**
	 * get the default log
	 */
	static private Log log = LogFactory.getLog(ExceptionLogger.class);

	static public final String TRACE = "trace";
	static public final String DEBUG = "debug";
	static public final String INFO = "info";
	static public final String WARN = "warn";
	static public final String ERROR = "error";
	static public final String FATAL = "fatal";

	/**
	 * 
	 * @param logtype
	 * @param message
	 * @param ex
	 */
	public static void log(String logtype, String message, Exception ex) {

		if (logtype == null)
			logtype = ERROR;

		else if (logtype.equals(DEBUG)) {
			log.debug(message, ex);
		} else if (logtype.equals(INFO)) {
			log.info(message, ex);
		} else if (logtype.equals(WARN)) {
			log.warn(message, ex);
		}
		if (logtype.equals(ERROR)) {
			log.error(message, ex);
		} else if (logtype.equals(FATAL)) {
			log.fatal(message, ex);
		}
		// logger.log(Priority.ERROR,"End of logger");
	}

	/**
	 * 
	 * @param logtype
	 * @param message
	 */
	public static void log(String logtype, String message) {
		if (logtype == null)
			logtype = INFO;

		else if (logtype.equals(DEBUG)) {
			log.debug(message);
		} else if (logtype.equals(INFO)) {
			log.info(message);
		} else if (logtype.equals(WARN)) {
			log.warn(message);
		}
		if (logtype.equals(ERROR)) {
			log.error(message);
		} else if (logtype.equals(FATAL)) {
			log.fatal(message);
		}
		// logger.log(Priority.ERROR,"End of logger");
	}

	/**
	 * 把Exception的StatckTrace打印到log日志中
	 * 
	 * @param e
	 * @param log
	 */
	public static void printExceptionStackTrace(Throwable e, Log log) {
		StackTraceElement[] s = e.getStackTrace();
		for (int i = 0; i < s.length; i++) {
			log.error("Exception:" + s[i].toString());
		}
	}

}
